package com.atguigu.flink.chapter10.join;

import org.apache.flink.runtime.state.storage.JobManagerCheckpointStorage;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2023/6/29 13:56
 */
public class InnerJoin {
    public static void main(String[] args) {
      
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
    
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(20));
//        tEnv.getConfig().set("table.exec.state.ttl", "20 second");
        
        tEnv.executeSql("create table t1(" +
                            " id string, " +
                            " name string " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 't1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'atguigu', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv' " +
                            ")");
    
        tEnv.executeSql("create table t2(" +
                            " id string, " +
                            " age int " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 't2', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'atguigu', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv' " +
                            ")");
        
        // 1. 内连接
        
        /*tEnv.sqlQuery("select " +
                          " t1.id, " +
                          " name," +
                          " age " +
                          "from t1 " +
                          "join t2 on t1.id=t2.id")
            .execute()
            .print();*/
        // select .. from a join b on a.id=b.id
        // select .. from a, b where a.id=b.id
    
        tEnv.sqlQuery("select " +
                          " t1.id, " +
                          " name," +
                          " age " +
                          "from t1, t2 where t1.id=t2.id")
            .execute()
            .print();
        
    }
}
/*
默认情况下, 所有数据永远存储到状态中(内存)
生产环境, 一定要给数据设置 ttl
 */